import { FormField, FieldRenderer } from '../types'
import { TextareaFieldRenderer } from './textareaField'
import { TagsFieldRenderer } from './tagsField'
import { ContextDisplayRenderer } from './contextDisplay'

export class FieldRendererFactory {
  private renderers: Map<string, FieldRenderer> = new Map()

  constructor() {
    this.renderers.set('textarea', new TextareaFieldRenderer())
    this.renderers.set('input', new TextareaFieldRenderer()) // 复用textarea渲染器
    this.renderers.set('tags', new TagsFieldRenderer())
  }

  getRenderer(type: string): FieldRenderer {
    const renderer = this.renderers.get(type)
    if (!renderer) {
      throw new Error(`不支持的字段类型: ${type}`)
    }
    return renderer
  }

  renderField(field: FormField): string {
    const renderer = this.getRenderer(field.type)
    return renderer.render(field)
  }

  getValidationScript(field: FormField): string {
    const renderer = this.getRenderer(field.type)
    return renderer.getValidationScript(field)
  }

  // 获取标签字段的初始化脚本
  getFieldScript(field: FormField): string {
    if (field.type === 'tags') {
      const tagsRenderer = this.renderers.get('tags') as TagsFieldRenderer
      return tagsRenderer.generateScript(field)
    }
    return ''
  }
}

export { ContextDisplayRenderer }
export * from './textareaField'
export * from './tagsField'
export * from './contextDisplay'
